package com.sjgame.model.controller;

import com.sjgame.base.common.PageByBootStarp;
import com.sjgame.base.controller.BaseController;
import com.sjgame.model.entity.Log;
import com.sjgame.model.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * create by zcl
 */
@Controller
@RequestMapping("/log")
public class LogController extends BaseController {
    @Autowired
    private LogService logService;
    @RequestMapping(value = {"/index"})
    public String index(ModelMap map,String account) {
//        Sort t  = new Sort(Sort.Direction.DESC,"createTime");
//        Page<Log> page = logService.findAll(new Specification<Log>() {
//            @Override
//            public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                Path<String> namepath = root.get("userName");
//                List<Predicate> list = new ArrayList<Predicate>();
//                if(account!=null&&!"".equals(account)) {
//                    list.add(cb.like(namepath,"%"+account+"%"));
//                }
//
//                Predicate[] p = new Predicate[list.size()];
//                return cb.and(list.toArray(p));
//            }
//        },getPageRequest(t));
//        map.put("pageInfo", page);
//        map.put("account", account);

        return "log/index";
    }

    @RequestMapping(value = {"/indexdata"})
    @ResponseBody
    public PageByBootStarp indexdata(ModelMap map, String account) {
        Sort t  = new Sort(Sort.Direction.DESC,"createTime");
        Page<Log> page = logService.findAll(new Specification<Log>() {
            @Override
            public Predicate toPredicate(Root<Log> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namepath = root.get("userName");
                List<Predicate> list = new ArrayList<Predicate>();
                if(account!=null&&!"".equals(account)) {
                    list.add(cb.like(namepath,"%"+account+"%"));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },getPageRequest(t));

        return new PageByBootStarp().formatResult(page);
    }

}
